﻿using Microsoft.Extensions.Logging;
using NetCore.Common.Redis;
using NetCore.IRepository;
using NetCore.IRepository.Order_LayerwinDingmachineEquipment;
using NetCore.IServices.Order_LayerwinDingmachineEquipment;
using NetCore.Model.Enity;
using NetCore.Model.VeiwModels;
using NetCore.Service;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NetCore.Services.Order_LayerwinDingmachineEquipment
{
    public class Order_LayerwinDingmachineEquipmentServices : BaseService<OrderLayerwindingmachineequipment>, IOrder_layerwindingmachineequipmentService
    {
        private readonly IMain_RedisCacheManager _redisCacheManager;
        private readonly IOrder_layerwindingmachineequipmentRepository _order_LayerwindingmachineequipmentRepository;
        private readonly ILogger<Order_LayerwinDingmachineEquipmentServices> _logger;

        public Order_LayerwinDingmachineEquipmentServices(IBaseRepository<OrderLayerwindingmachineequipment> baseRepository, IMain_RedisCacheManager redisCacheManager, ILogger<Order_LayerwinDingmachineEquipmentServices> logger, IOrder_layerwindingmachineequipmentRepository order_LayerwindingmachineequipmentRepository) : base(baseRepository)
        {
            _redisCacheManager = redisCacheManager;
            _logger = logger;
            _order_LayerwindingmachineequipmentRepository = order_LayerwindingmachineequipmentRepository;
        }

        /// <summary>
        /// 缓存添加班次比例
        /// </summary>
        /// <returns></returns>
        public async Task<bool> AddShiftProportionRedis()
        {
            try
            {
                //总条数
                int total_count = await _order_LayerwindingmachineequipmentRepository.GetTotalNumber(0);
                //早班条数
                int morningShift_count = await _order_LayerwindingmachineequipmentRepository.GetTotalNumber(5);
                //中班条数
                int middleShift_count = await _order_LayerwindingmachineequipmentRepository.GetTotalNumber(6);
                //晚班条数
                int eveningShift_count = await _order_LayerwindingmachineequipmentRepository.GetTotalNumber(7);
                GetShiftProportionResult getShiftProportionResult = new GetShiftProportionResult()
                {
                    Total_count = total_count,
                    MorningShift_count = morningShift_count,
                    MiddleShift_count = middleShift_count,
                    EveningShift_count = eveningShift_count,
                    MorningShiftPercentage = Convert.ToDouble(morningShift_count) / Convert.ToDouble(total_count) + "%",
                    MiddleShiftPercentage = Convert.ToDouble(middleShift_count) / Convert.ToDouble(total_count) + "%",
                    EveningShiftPercentage = Convert.ToDouble(eveningShift_count) / Convert.ToDouble(total_count) + "%"
                };
                _redisCacheManager.Set("AddShiftProportionRedis", JsonConvert.SerializeObject(getShiftProportionResult, Formatting.Indented), (DateTime.Now - DateTime.Now.AddMinutes(30)).Duration());
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
                return false;
            }
        }

        /// <summary>
        /// 统计指定月份每天数据
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <param name="equipmentId">设备id</param>
        /// <returns></returns>
        public async Task<List<DayCount>> Month_StatisticsDailyData(Month_StatisticsDailyDataFrom statisticsDailyDataFrom)
        {
            List<DayCount> dayCounts = new List<DayCount>();
            try
            {
                //查询redis中是否有数据
                if (_redisCacheManager.ContainsKey(RedisKey.StatisticsDailyData(statisticsDailyDataFrom.Year, statisticsDailyDataFrom.Month, statisticsDailyDataFrom.Shiftid, statisticsDailyDataFrom.Equipmentid, statisticsDailyDataFrom.Userid)))
                    return _redisCacheManager.Get<List<DayCount>>(RedisKey.StatisticsDailyData(statisticsDailyDataFrom.Year, statisticsDailyDataFrom.Month, statisticsDailyDataFrom.Shiftid, statisticsDailyDataFrom.Equipmentid, statisticsDailyDataFrom.Userid));
                //统计指定月份每天数据
                dayCounts = await _order_LayerwindingmachineequipmentRepository.Month_StatisticsDailyData(statisticsDailyDataFrom);
                //写入Redis
                _redisCacheManager.Set(RedisKey.StatisticsDailyData(statisticsDailyDataFrom.Year, statisticsDailyDataFrom.Month, statisticsDailyDataFrom.Shiftid, statisticsDailyDataFrom.Equipmentid, statisticsDailyDataFrom.Userid), JsonConvert.SerializeObject(dayCounts, Formatting.Indented), (DateTime.Now - DateTime.Now.AddMinutes(30)).Duration());
                return dayCounts;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
                return dayCounts;
            }
        }

        /// <summary>
        /// 统计指定年份月天数据
        /// </summary>
        /// <returns></returns>
        public async Task<bool> Year_StatisticsDailyData(Year_StatisticsDailyDataFrom year_StatisticsDaily)
        {
            try
            {
                //统计指定月份每天数据
                List<MonthCount> monthCounts = await _order_LayerwindingmachineequipmentRepository.Year_StatisticsDailyData(year_StatisticsDaily);
                //写入Redis
                _redisCacheManager.Set("StatisticsDailyData_" + year_StatisticsDaily.Year + "年" + year_StatisticsDaily.Shiftid + "班次" + year_StatisticsDaily.Equipmentid + "设备" + year_StatisticsDaily.Userid + "用户", JsonConvert.SerializeObject(monthCounts, Formatting.Indented), (DateTime.Now - DateTime.Now.AddMinutes(30)).Duration());
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
                return false;
            }
        }
    }
}